/**
 * @Author Fizz Pu
 * @Date 2020/9/24 下午8:35
 * @Version 1.0
 * 失之毫厘，缪之千里！
 */

import java.util.Deque;
import java.util.LinkedList;

/**
 * 定义栈的数据结构，请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中，调用 min、push 及 pop 的时间复杂度都是 O(1)。
 *
 *  
 *
 * 示例:
 *
 * MinStack minStack = new MinStack();
 * minStack.push(-2);
 * minStack.push(0);
 * minStack.push(-3);
 * minStack.min();   --> 返回 -3.
 * minStack.pop();
 * minStack.top();      --> 返回 0.
 * minStack.min();   --> 返回 -2.
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */


public class Lee30 {

}

class MinStack {
    Deque<Integer> mainStack = new LinkedList<>();
    Deque<Integer> supportedStack = new LinkedList<>();

    /** initialize your data structure here. */
    public MinStack() {

    }

    public void push(int x) {
        mainStack.push(x);
        int val = x;
        if(!supportedStack.isEmpty() && val > supportedStack.peek()) {
            supportedStack.push(supportedStack.peek());
        }else{
            supportedStack.push(val);
        }
    }

    public void pop() {
        mainStack.pop();
        supportedStack.pop();
    }

    public int top() {
        return mainStack.peek();
    }

    public int min() {
        return supportedStack.peek();
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.min());
        minStack.pop();
        System.out.println(minStack.top());
        System.out.println(minStack.min());
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.min();
 */